﻿@{
    Layout = null;
}

<div>
    <div class="input-group">
        <div class="col-3 pr-0" style="display: none">
            <label id="SAMPLE-addMaterial-MaterialId">@ViewData["MaterialId"]</label>
            <label id="SAMPLE-addMaterial-Parent-ModalID">@ViewData["ModalID"]</label>
        </div>
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">代码*</span>
        </div>
        <div class="col-9 pl-0">
            <input id="SAMPLE-addMaterial-Code" class="form-control font-rfs border-radius-noleft" placeholder="必填" />
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">名称*</span>
        </div>
        <div class="col-9 pl-0">
            <input id="SAMPLE-addMaterial-Name" class="form-control font-rfs border-radius-noleft" placeholder="必填" />
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">一级分类*</span>
        </div>
        <div class="col-9 pl-0 input-group position-relative">
            <select id="SAMPLE-addMaterial-Select-Type1" class="form-control font-rfs border-radius-noleft" style="width:90%"></select>
            <div class="col-11 pl-0 input-group position-absolute float-left"><input id="SAMPLE-addMaterial-Input-Type1" class="form-control font-rfs border-radius-noleft noRightBorder" style="width:100%" placeholder="可输入新的类型" /></div>
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">二级分类</span>
        </div>
        <div class="col-9 pl-0 input-group position-relative">
            <select id="SAMPLE-addMaterial-Select-Type2" class="form-control font-rfs border-radius-noleft" style="width:90%"></select>
            <div class="col-11 pl-0 input-group position-absolute float-left"><input id="SAMPLE-addMaterial-Input-Type2" class="form-control font-rfs border-radius-noleft noRightBorder" style="width:100%" placeholder="可输入新的类型" /></div>
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">设备类型</span>
        </div>
        <div class="col-9 pl-0 input-group position-relative">
            <select id="SAMPLE-addMaterial-Select-EquipmentType" class="form-control font-rfs border-radius-noleft"></select>
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">设备编号</span>
        </div>
        <div class="col-9 pl-0 input-group position-relative">
            <select id="SAMPLE-addMaterial-Select-EquipmentId" class="form-control font-rfs border-radius-noleft"></select>
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">高限</span>
        </div>
        <div class="col-9 pl-0 input-group">
            <input id="SAMPLE-addMaterial-HighLimit" class="form-control font-rfs border-radius-noleft" style="width:45%" value="0.00" />
            <span class="input-group-text font-rfs border-radius-noradius noRightBorder noLeftBorder" style="width:23%">低限</span>
            <input id="SAMPLE-addMaterial-LowLimit" class="form-control font-rfs border-radius-noleft" style="width:32%" value="0.00" />
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">备注</span>
        </div>
        <div class="col-9 pl-0">
            <input id="SAMPLE-addMaterial-Note" class="form-control font-rfs border-radius-noleft" placeholder="可选" />
        </div>
    </div>
    <div class="text-right">
        <button id="Supplier-AddSupplier-confirm" class="btn btn-primary font-rfs">确定</button>
        <button id="Supplier-AddSupplier-cancel" type="button" class="btn btn-info font-rfs">取消</button>
    </div>
</div>

<script>

    $(document).ready(function () {
        LoadType1();
        LoadEquipmentType();
        if ($("#SAMPLE-addMaterial-MaterialId").html().length > 0) {
            LoadMaterial();
        }
    });

    function LoadType1() {
        $.post("GetMaterialType1", {
        }, function (data) {
            for (var i = 0; i < data.length; i++) {
                $("#SAMPLE-addMaterial-Select-Type1").append(new Option(data[i].type1));
            }
            $("#SAMPLE-addMaterial-Select-Type1").prepend(new Option("", ""));
            $("#SAMPLE-addMaterial-Select-Type1").val("");
        });
    }

    function LoadEquipmentType() {
        $.post("\\Eq\\GetEquipmentTypeList", {
        }, function (data) {
            for (var i = 0; i < data.length; i++) {
                $("#SAMPLE-addMaterial-Select-EquipmentType").append(new Option(data[i], data[i]));
            }
            $("#SAMPLE-addMaterial-Select-EquipmentType").prepend(new Option("", ""));
            $("#SAMPLE-addMaterial-Select-EquipmentType").val("");
        });
    }

    function LoadMaterial() {
        $.post("GetMaterialByCode", {
            Code: $("#SAMPLE-addMaterial-MaterialId").html()
        }, function (data) {
            $("#SAMPLE-addMaterial-Code").val(data.code);
            $("#SAMPLE-addMaterial-Code").attr("readonly", true);
            $("#SAMPLE-addMaterial-Name").val(data.name);
            $("#SAMPLE-addMaterial-Input-Type1").val(data.type1);
            $("#SAMPLE-addMaterial-Input-Type2").val(data.type2);
            $("#SAMPLE-addMaterial-Select-EquipmentType").val(data.equipmentType);
            $("#SAMPLE-addMaterial-Select-EquipmentId").prepend(new Option(data.equipmentId));
            $("#SAMPLE-addMaterial-Note").val(data.note);
            $("#SAMPLE-addMaterial-HighLimit").val(data.highLimit);
            $("#SAMPLE-addMaterial-LowLimit").val(data.lowLimit);
        });
    }

    $("#SAMPLE-addMaterial-Select-Type1").change(function () {
        $('#SAMPLE-addMaterial-Input-Type1').val($("#SAMPLE-addMaterial-Select-Type1").find("option:selected").text());
        $.post("GetMaterialType2ByType1", {
            type1: $("#SAMPLE-addMaterial-Select-Type1").find("option:selected").text()
        }, function (data) {
            $("#SAMPLE-addMaterial-Select-Type2").empty();
            for (var i = 0; i < data.length; i++) {
                $("#SAMPLE-addMaterial-Select-Type2").append(new Option(data[i].type2));
            }
            $("#SAMPLE-addMaterial-Select-Type2").prepend(new Option("", ""));
            $("#SAMPLE-addMaterial-Select-Type2").val("");
        });
    });

    $("#SAMPLE-addMaterial-Select-Type2").change(function () {
        $('#SAMPLE-addMaterial-Input-Type2').val($("#SAMPLE-addMaterial-Select-Type2").find("option:selected").text());
    });

    $("#SAMPLE-addMaterial-Select-EquipmentType").change(function () {
        if ($("#SAMPLE-addMaterial-Select-EquipmentType").find("option:selected").text().length != 0) {
            $.post("\\Eq\\GetEquipmentIdsByType", {
                type: $("#SAMPLE-addMaterial-Select-EquipmentType").find("option:selected").text()
            }, function (data) {
                $("#SAMPLE-addMaterial-Select-EquipmentId").empty();
                for (var i = 0; i < data.length; i++) {
                    $("#SAMPLE-addMaterial-Select-EquipmentId").append(new Option(data[i]));
                }
                $("#SAMPLE-addMaterial-Select-EquipmentId").prepend(new Option("", ""));
                $("#SAMPLE-addMaterial-Select-EquipmentId").val("");
            });
        }
    });

    $("#Supplier-AddSupplier-confirm").click(function () {
        if ($.trim($("#SAMPLE-addMaterial-Code").val()).length == 0) {
            alert("物料代码不能为空！");
            $("#SAMPLE-addMaterial-Code").addClass('error-style');
            return false;
        }
        if ($.trim($("#SAMPLE-addMaterial-Name").val()).length == 0) {
            alert("物料名称不能为空！");
            $("#SAMPLE-addMaterial-Name").addClass('error-style');
            return false;
        }
        if ($.trim($("#SAMPLE-addMaterial-Input-Type1").val()).length == 0) {
            alert("一级分类不能为空！");
            $("#SAMPLE-addMaterial-Input-Type1").addClass('error-style');
            return false;
        }

        var jsonStr = {};
        jsonStr["Code"] = $.trim($("#SAMPLE-addMaterial-Code").val());
        jsonStr["Name"] = $.trim($("#SAMPLE-addMaterial-Name").val());
        jsonStr["Type1"] = $.trim($("#SAMPLE-addMaterial-Input-Type1").val());
        jsonStr["Type2"] = $.trim($("#SAMPLE-addMaterial-Input-Type2").val());
        jsonStr["EquipmentType"] = $.trim($("#SAMPLE-addMaterial-Select-EquipmentType").find("option:selected").text());
        jsonStr["EquipmentId"] = $.trim($("#SAMPLE-addMaterial-Select-EquipmentId").find("option:selected").text());
        jsonStr["Note"] = $.trim($("#SAMPLE-addMaterial-Note").val());
        jsonStr["HighLimit"] = $.trim($("#SAMPLE-addMaterial-HighLimit").val());
        jsonStr["LowLimit"] = $.trim($("#SAMPLE-addMaterial-LowLimit").val());

        var AddMaterial_Id = $("#SAMPLE-addMaterial-MaterialId").html();
        if (AddMaterial_Id == "") {
            $.post("postAddMaterial", {
                jsonStr: JSON.stringify(jsonStr)
            }, function (data) {
                alert(data);
            });
        } else {
            $.post("UpdateMaterial", {
                jsonStr: JSON.stringify(jsonStr)
            }, function (data) {
                if (data.indexOf("成功") >= 0) {
                    var modalID = $("#SAMPLE-addMaterial-Parent-ModalID").html();
                    $("#" + modalID).modal('hide');
                } else {
                    alert(data);
                }
            });
        }
    });

    $("#Supplier-AddSupplier-cancel").click(function () {
        var modalID = $("#SAMPLE-addMaterial-Parent-ModalID").html();
        $("#" + modalID).modal('hide');
    });

</script>
